package cn.fl.atc.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.api.ISysRegionApiService;
import cmm.comm.facade.framework.dto.SysRegionDTO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.atc.service.IAtcBscInfoService;
import cn.fl.atc.transform.AtcBscInfoVOTransform;
import cn.fl.atc.vo.AtcBscInfoVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import fl.spl.facade.atc.api.IAtcBscInfoApiService;
import fl.spl.facade.atc.api.IAtcRlPsnApiService;
import fl.spl.facade.atc.condition.AtcRlPsnQueryCondition;
import fl.spl.facade.atc.constant.AtcBscInfoConstant;
import fl.spl.facade.atc.dto.AtcBscInfoDTO;
import fl.spl.facade.atc.dto.AtcRlPsnDTO;

/**
 * @Description: 汽贸城基础信息表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class AtcBscInfoServiceImpl implements IAtcBscInfoService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(AtcBscInfoServiceImpl.class);

	/**
	 * TODO 自定义 API 暴露服务
	 */
	@Autowired
	private IAtcBscInfoApiService atcBscInfoApiService;
	
	@Autowired
	private ISysRegionApiService sysRegionApiService;
	
	@Autowired
	private IAtcRlPsnApiService atcRlPsnApiService;
	@Override
	public AtcBscInfoVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		AtcBscInfoDTO dto = atcBscInfoApiService.searchById(id);
		return AtcBscInfoVOTransform.toVO(dto);
	}

	@Override
	public Long add(AtcBscInfoVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		return atcBscInfoApiService.addForUpdateAtcBscInfo(AtcBscInfoVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.atcBscInfoApiService.removeById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
    		BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(AtcBscInfoVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.atcBscInfoApiService.modify(AtcBscInfoVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return atcBscInfoApiService.modifyAtcBscInfo(AtcBscInfoVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)
			page = new Pager();
		Pager pager = atcBscInfoApiService.searchAtcBscInfoPager(page);
		List<AtcBscInfoDTO> listDto = (List<AtcBscInfoDTO>) pager.getResultList();
		List<AtcBscInfoVO> list = AtcBscInfoVOTransform.toVOList(listDto);
		if(CollectionUtils.isNotEmpty(list)){
			//当前时间 + 30天
			Date date = new Date();
			Calendar c = Calendar.getInstance();
	        c.setTime(date);
	        c.add(Calendar.DAY_OF_MONTH, 30);
			//获取联系人信息
			List<Long> atcIdList = new ArrayList<>();
			List<String> rgnPrList = new ArrayList<>();
			List<String> rgnCyList = new ArrayList<>();
			for (AtcBscInfoVO vo : list) {
				if(vo.getEndDt().before(date)){
					vo.setIsExpireRed(1);
				}else{
					if(!c.getTime().before(vo.getEndDt())){
						vo.setIsExpireBlue(1);
					}
				}
				if(vo.getStatus() == AtcBscInfoConstant.IS_STATUS_0){
					vo.setStatusNm("合作中");
				}else{
					vo.setStatusNm("已过期");
				}
				atcIdList.add(vo.getId());
				rgnPrList.add(vo.getRgnPrCd());
				rgnCyList.add(vo.getRgnCyCd());
			}
			AtcRlPsnQueryCondition qc = new AtcRlPsnQueryCondition();
			qc.setIsDefault(AtcBscInfoConstant.IS_STATUS_1);
			qc.setAtcIdList(atcIdList);
			qc.setIsDel(AtcBscInfoConstant.IS_STATUS_0);
			List<AtcRlPsnDTO> searchAtcRlPsnList = this.atcRlPsnApiService.searchAtcRlPsnList(qc);
			Map<Long,AtcRlPsnDTO> map = new HashMap<>();
			if(CollectionUtils.isNotEmpty(searchAtcRlPsnList)){
				for (AtcRlPsnDTO atcRlPsnDTO : searchAtcRlPsnList) {
					map.put(atcRlPsnDTO.getAtcId(), atcRlPsnDTO);
				}
			}
			List<String> rgnCdList = new ArrayList<>();
			rgnCdList.addAll(rgnCyList);
			rgnCdList.addAll(rgnPrList);
			Map<String, SysRegionDTO> rgnMap = this.sysRegionApiService.searchRegionByRgnCd(rgnCdList, CurrentThreadContext.getCurrentSysCd());
			if(CollectionUtils.isMapEmpty(rgnMap)){
				rgnMap = new HashMap<>();
			}
			for (AtcBscInfoVO vo : list) {
				//联系人信息
				if(map.containsKey(vo.getId())){
					AtcRlPsnDTO dto = map.get(vo.getId());
					vo.setRlNm(dto.getRlNm());
					vo.setMp(dto.getMp());
				}
				//区域
				if(rgnMap.containsKey(vo.getRgnCyCd())){
					SysRegionDTO dto = rgnMap.get(vo.getRgnCyCd());
					vo.setRgnCyCdNm(dto.getRgnNm());
				}
				if(rgnMap.containsKey(vo.getRgnPrCd())){
					SysRegionDTO dto = rgnMap.get(vo.getRgnPrCd());
					vo.setRgnPrCdNm(dto.getRgnNm());
				}
			}
		}
		pager.setResultList(list);
		return pager;
	}
}
